package formatter;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.text.DateFormat;
import java.text.NumberFormat;
import java.util.Currency;
import java.util.Locale;
import java.util.ResourceBundle;

import banking.*;

public class TextHtmlFormatter implements IStatementFormater {

	private StringBuffer sb;
	ResourceBundle bundle;
	Locale currentLocale;
	NumberFormat numberFormatter;
	DateFormat dateFormatter;
	private boolean finished = false;
	Currency currency;
	
	
	public TextHtmlFormatter(){
		sb = new StringBuffer("<html>\n" + "<head>\n" + "<title>HTML</title>\n"
				+ "</head>\n" + "<body>\n");
	}
	
	@Override
	public void addData(Account account) {
		
		currentLocale = new Locale(account.getCountry(), account.getLanguage());
		bundle = ResourceBundle.getBundle("props/local", currentLocale);
		numberFormatter = NumberFormat.getCurrencyInstance(currentLocale);
		dateFormatter = DateFormat.getDateTimeInstance(DateFormat.FULL,
				DateFormat.MEDIUM, currentLocale);
		currency = Currency.getInstance(currentLocale);

		sb.append(bundle.getString("accountNumber") + ":" + account.getAccountNumber() + "<br />");
		sb.append(bundle.getObject("name") + ":" + account.getAccountHolder() + "<br /> <br />");
		//evtl noch alten Kontostand als variable einfügen
		for (Statement str : account.getBookList()) {
			sb.append(bundle.getObject("date") + ": " + dateFormatter.format(str.getDate())+ "<br />");
			sb.append(bundle.getObject("reason") + ": " + str.getReason()+ " ");
			sb.append(bundle.getObject("value") + ": " + str.getValue() + " " + currency.getSymbol() + "<br />");
			}
	}
		
		
		public String getFormattedText(){
			if(!finished){
				sb.append("</body>\n" + 
						  "</html>");
				finished = true;
			}
			
			try {
	            BufferedWriter out = new BufferedWriter(new FileWriter(System.getProperty("user.home") + "/text.html"));
	            out.write(sb.toString());
	            out.close();
	        } catch (IOException e) {
	        	System.err.println("Could not write File. No Access?");
	        }
			return sb.toString();
		}
	
}